﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace FileParser
{
    /// <summary>
    /// This class deals entirely with interfacing with the doxygen
    /// program by creating a doxygen config fily (doxyfile). This
    /// is individual for each SVN accessed, and some info from the
    /// SVN must be acquired before it can be generated.
    /// </summary>
    public class C_DoxyFile
    {
        /// <summary>
        /// The single static function representing this class, it generates
        /// a doxygen config file for use with doxygen. It outputs to the 
        /// same directory as the exe a file called "doxyfile" that represents
        /// the doxygen config file. In order to generate the file, this class
        /// must read in a pair of data files, called "append1.txt" and
        /// "append2.txt" that represent most of the text in the doxygen config
        /// file.
        /// </summary>
        /// <param name="projname">
        /// This mostly obsolete variable covers the "Project Name" field in the
        /// doxygen generation file. It mostly shows up in the HTL output (which
        /// is not used)
        /// </param>
        /// <param name="address">
        /// Address is the address of the source code, for use with the
        /// doxygen config file field. If this is incorrect, doxygen
        /// will throw an error or generate blank documents
        /// </param>
        /// <returns>
        /// Returns a boolean representing the sucess of an operation.
        /// If there is an error due to missing files or failed reads,
        /// it will return false. A successful generation returns true.
        /// </returns>
        public static Boolean GenDConfig(string projname, string address)
        {
            //This list represents the final contents of the doxygen
            // config file
            List<string> outList = new List<string>();

            outList.Add("DOXYFILE_ENCODING      = UTF-8");
            outList.Add("project_NAME           = \"" + projname + "\"");
            outList.Add("project_NUMBER         = ");
            outList.Add("OUTPUT_DIRECTORY       = \"" + 
                Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly()
                .GetName().CodeBase).Substring(6) + "\"");

            //This is the first set of generation file after the header
            StreamReader reader = new StreamReader("append1.txt");
            try
            {
                do
                {
                    outList.Add(reader.ReadLine());
                }
                while (reader.Peek() != -1);
            }

            catch
            {
                return false;
                //TODO - Some error or another
            }

            finally
            {
                reader.Close();
            }

            outList.Add("INPUT                  = \"" + address + "\"");

            //This is the second set of pre-created doxyfile output.
            reader = new StreamReader("append2.txt");
            try
            {
                do
                {
                    outList.Add(reader.ReadLine());
                }
                while (reader.Peek() != -1);
            }

            catch
            {
                return false;
                //TODO - Some error or another
            }

            finally
            {
                reader.Close();
            }


            //Now that we have the output in the list, output it to the doxy gen
            // file we'll use later

            FileInfo t = new FileInfo("Doxyfile");
            StreamWriter Twriter = t.CreateText();
            foreach (string s in outList)
                Twriter.WriteLine(s);
            
            Twriter.Write(Twriter.NewLine);
            Twriter.Close();

            return true;
        }
    }
}
